﻿using System;
using System.Windows.Forms;
using EFERP;
using HRM.DICTIONARY.DAL;

namespace HRM.DICTIONARY.UI
{
    public partial class xucSymbol : DevExpress.XtraEditors.XtraUserControl
    {
       private ICommon<dic_symbol> _exeDic;
        private dic_symbol _currObj;
        private bool _isChooseRow;

        public xucSymbol()
        {
            InitializeComponent();
            this.Focus();
        }

        private void xucSymbol_Load(object sender, EventArgs e)
        {
            _exeDic = new Common<dic_symbol>(StaticVariable.ERPEn);
            _exeDic.GV = gbList;
            _exeDic.GC = gcList;
            _exeDic.ColIDName = "symbolcode"; 
            Reload();
        }
        private void Add()
        {
            xucSymbolAdd frm = new xucSymbolAdd(_exeDic);
            frm.ShowDialog();
            if (_exeDic.myAction == Actions.Insert || _exeDic.myAction == Actions.InsertMultiple)
            {
                _exeDic.myAction = Actions.View;
            }
            Reload();
        }

        private void Edit()
        {
            xucSymbolAdd frm = new xucSymbolAdd(_currObj, _exeDic);
            frm.ShowDialog();
            if (_exeDic.myAction == Actions.Update)
            {
                _exeDic.myAction = Actions.View;Reload();
            }
            _isChooseRow = false;
            DoShowOrHide(_isChooseRow);
        }

        private void Delete()
        {
            bool kq = _exeDic.DeleteOnGridView(_currObj);
            if (kq)
            {
                _isChooseRow = false;
                DoShowOrHide(_isChooseRow);
            }
            else
            {
                MessageBox.Show("Không thể xóa dữ liệu");
            }
        }

        private void Reload()
        {
            if (_exeDic != null)
            {
                _exeDic.AddGridControlFromList();
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }

        public void Print()
        {
            if (gbList != null)
            {
                if (gbList.RowCount > 0)
                {
                    gbList.ShowPrintPreview();
                    _isChooseRow = false;
                    _currObj = null;
                    DoShowOrHide(_isChooseRow);
                }
            }
        }

        public void Export()
        {
            if (_exeDic != null)
            {
                _exeDic.AddGridExport(this);
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }

        private void DoShowOrHide(bool isChoose)
        {
            bbiEdit.Enabled = isChoose;
            bbiDelete.Enabled = isChoose;
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Dispose();
                    return true;

                case Keys.F1:
                    //Help Link Click
                    return true;

                case Keys.F5:
                    this.Reload();
                    return true;

                case (Keys.Control | Keys.E):
                    this.Export();
                    return true;

                case (Keys.Control | Keys.F):
                    //Search
                    return true;

                case (Keys.Control | Keys.N):
                    Add();
                    return true;

                case (Keys.Enter):
                    Edit();
                    return true;

                case (Keys.Control | Keys.X):
                    Delete();
                    return true;

                case (Keys.Control | Keys.P):
                    this.Print();
                    return true;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    return true;
            }
            return false;
        }

        #region Events

        private void bbiAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
        }

        private void bbiEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }

        private void bbiDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }

        private void bbiRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Reload();
        }

        private void bbiPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Print();
        }

        private void bbiExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Export();
        }

        private void bbiClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Dispose();
        }

        private void gbList_Click(object sender, EventArgs e)
        {
            _currObj = _exeDic.gbList_Click(sender, e);
            if (_currObj == null)
            {
                MessageBox.Show("Vui lòng chọn dòng trên bảng tính");
            }
            else
            {
                _isChooseRow = true;
                DoShowOrHide(_isChooseRow);
            }
        }

        private void gbList_DoubleClick(object sender, EventArgs e)
        {
            _currObj = _exeDic.gbList_Click(sender, e);
            if (_currObj == null)
            {
                MessageBox.Show("Vui lòng chọn dòng trên bảng tính");
            }
            else
            {Edit();
            }
        }

        public void xucDegree_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    Dispose();
                    break;

                case Keys.F1:
                    //Help Link Click

                case Keys.F5:
                    this.Reload();
                    break;

                case (Keys.Control | Keys.E):
                    this.Export();
                    break;

                case (Keys.Control | Keys.F):
                    //Search
                    break;

                case (Keys.Control | Keys.N):
                    //Call Form Add
                    break;

                case (Keys.Control | Keys.P):
                    this.Print();
                    break;

                case (Keys.Alt | Keys.D):
                    MessageBox.Show("You are master ");
                    break;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    break;
            }
        }

        private void xucDegree_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void xucDegree_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {

        }
        #endregion
    }

    //
}
